package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("goods/listInventory")
    public Map listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        Map<String, Object> map = new HashMap<>();
        PageInfo<Goods> pageResult = goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getList());
        return map;
    }

    // http://localhost:8080/goods/list
    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("goods/list")
    public Map getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        Map<String, Object> map = new HashMap<>();
        PageInfo<Goods> pageResult = goodsService.getGoodsList(page,rows,goodsName,goodsTypeId);
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getList());
        return map;
    }

    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    // http://localhost:8080/goods/save?goodsId=37
    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("goods/save")
    public ServiceVO save(Goods goods){
        goodsService.save(goods);
        return new ServiceVO(100,"请求成功");
    }

    //http://localhost:8080/goods/delete
    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("goods/delete")
    public ServiceVO delete(Integer goodsId){
        goodsService.delete(goodsId);
        return new ServiceVO(100,"请求成功");
    }


    // http://localhost:8080/goods/getNoInventoryQuantity
    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("goods/getNoInventoryQuantity")
    public Map getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }


    // http://localhost:8080/goods/getHasInventoryQuantity
    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("goods/getHasInventoryQuantity")
    public Map getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }


    // http://localhost:8080/goods/saveStock?goodsId=25
    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("goods/saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);

        return new ServiceVO(100,"请求成功");
    }


    // http://localhost:8080/goods/deleteStock
    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("goods/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        goodsService.deleteStock(goodsId);
        return new ServiceVO(100,"请求成功");
    }


    // // http://localhost:8080/goods/listAlarm
    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("goods/listAlarm")
    public Map listAlarm(){
        Map<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsService.listAlarm();
        map.put("rows",goodsList);
        return map;
    }


    // http://localhost:8080/unit/list
    /**
     * 查询所有商品单位
     * @return
     */
    @PostMapping("unit/list")
    public Map unitList(){
        Map<String, Object> map = new HashMap<>();
        List<Unit> unitList = goodsService.unitList();
        map.put("rows",unitList);
        return map;
    }

}
